<template>
  <!-- 用户名密码登录 -->
  <el-form class="login-form" ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on"
           label-position="left">
    <el-form-item prop="username">
	    <span class="svg-container">
	      <svg-icon icon-class="user"/>
	    </span>
      <el-input
        ref="username"
        v-model.trim="loginForm.username"
        :placeholder="$t('login.username')"
        name="username"
        type="text"
        tabindex="1"
        auto-complete="on"
      />
    </el-form-item>
    <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
      <el-form-item prop="password">
	      <span class="svg-container">
	        <svg-icon icon-class="password"/>
	      </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model.trim="loginForm.password"
          :type="passwordType"
          :placeholder="$t('login.password')"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.native="checkCapslock"
          @blur="capsTooltip = false"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
	        <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
	      </span>
      </el-form-item>
    </el-tooltip>
    <el-form-item prop="code" style="width: 65%;" v-if="codeShow">
		  <span class="svg-container">
		    <svg-icon icon-class="code"/>
		  </span>
      <el-input
        ref="code"
        v-model.trim="loginForm.code"
        placeholder="图形码"
        name="code"
        type="text"
        tabindex="1"
        maxlength="4"
        auto-complete="on"
        style="width: 73%;"
      />
      <sIdentify :identifyCode="identifyCode" @refreshCode="refreshCode"></sIdentify>
    </el-form-item>
    <el-button :loading="loading" type="primary"
               style="width:100%;margin-bottom:30px;margin-top: 20px;padding: 14px 20px;background: #6dcdb0;border: 1px solid #6dcdb0;"
               @click.native.prevent="handleLogin">{{ $t('login.logIn') }}
    </el-button>
  </el-form>
</template>

<script>
import sIdentify from '@/views/login/components/identify'
import {Message} from 'element-ui';

export default {
  components: {sIdentify},
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value == '') {
        Message({
          message: '请输入账号',
          type: 'error',
          duration: 4 * 1000
        })
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        Message({
          message: '密码长度必须大于6位数',
          type: 'error',
          duration: 4 * 1000
        })
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value == '') {
        Message({
          message: '请输入图形码',
          type: 'error',
          duration: 4 * 1000
        })
      } else {
        callback()
      }
    }
    return {
      codeShow: false,
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'submit', validator: validateUsername}],
        password: [{required: true, trigger: 'submit', validator: validatePassword}],
        code: [{required: true, trigger: 'submit', validator: validateCode}],
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      identifyCodes: "1234567890",
      identifyCode: ""
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
    //图形码
    this.identifyCode = "";
    this.makeCode(this.identifyCodes, 4);
  },
  methods: {
    checkCapslock({shiftKey, key} = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (this.codeShow && this.loginForm.code != this.identifyCode) {
            this.loginForm.code = '';
            Message({
              message: '图形码不正确',
              type: 'error',
              duration: 4 * 1000
            })
            this.refreshCode();
            return;
          }
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({path: this.redirect || '/'})
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //图形码
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
          this.randomNum(0, this.identifyCodes.length)
          ];
      }
      console.log(this.identifyCode);
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    }
  }
}
</script>

<style scoped lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 83%;

    input {
      //background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 20px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  .logo {
    width: 160px;
    height: 160px;
    margin-bottom: 30px;
  }

  .copyright {
    color: #fff;
    opacity: 0.4;
    font-size: 12px;
    margin-top: 20px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 20px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

.s-canvas canvas {
  position: absolute;
  right: -156px;
  top: 1px;
  cursor: pointer;
}
</style>
